package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpYAccountCustomerDetailsNotesTabPageUtil extends SeleniumPageUtil {
	
	public final static By CDNT_SPAN_MAIN_HEADER = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_Header_lblHeader");
	public final static By CDNT_IMG_REPORT_PREFERENCES = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_Header_imgPreferences");
	public final static By CDNT_IMG_EXPORT = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_Header_imgExcelExport");
	public final static By CDNT_LINK_NEW = By.xpath("//*[@id='e0dce11b-40ad-4118-b5d9-84bb06c2b270']/a");
	public final static By CDNT_TD_PAGE = By.xpath("//*[@id='dvReport']/table[2]/tbody/tr/td[1]");
	public final static By CDNT_SPAN_PAGE_START_NO = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_PagingTop_lblPagingPageNumber");
	public final static By CDNT_TD_OF = By.xpath("//*[@id='dvReport']/table[2]/tbody/tr/td[3]");
	public final static By CDNT_SPAN_PAGE_COUNT = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_PagingTop_lblPagingPageCount");
	public final static By CDNT_INPUT_FIRST = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_PagingTop_imgFirst");
	public final static By CDNT_INPUT_PREVIOUS = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_PagingTop_imgPrevious");
	public final static By CDNT_INPUT_NEXT = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_PagingTop_imgNext");
	public final static By CDNT_INPUT_LAST = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_PagingTop_imgLast");
	public final static By CDNT_IMG_FILTER_0 = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_filter_0");
	public final static By CDNT_IMG_FILTER_1 = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_filter_1");
	public final static By CDNT_IMG_FILTER_2 = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_filter_2");
	public final static By CDNT_IMG_FILTER_3 = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_filter_3");
	public final static By CDNT_TH_CONSUMER = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_sort_0");
	public final static By CDNT_TH_DATE_CREATED = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_sort_1");
	public final static By CDNT_TH_CREATED_BY = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_sort_2");
	public final static By CDNT_TH_NOTE = By.xpath("//*[@id='ParentChildContainer_CustomerDetails_Notes1_NotesReport_ReportOutput']/table/tbody/tr[2]/th[4]");
	
	// elements of filter pop-up window
	public final static By CDNT_INPUT_FILTER_TEXT = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_txtFilterInput");
	public final static By CDNT_INPUT_ENTER = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_btnFilterEnter");
	public final static By CDNT_INPUT_CANCEL = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_btnFilterCancel");
	public final static By CDNT_INPUT_CLEAR = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_btnFilterClear");
	
	// page refresh icon
	public final static By CDNT_IMG_REFRESH = By.id("ParentChildContainer_CustomerDetails_Notes1_NotesReport_Header_imgRefresh");
	
	// elements under New note page
	public final static By CDNT_LINK_SAVE = By.xpath("//*[@id='0790172a-861a-45e1-a408-ef42dcc1dcb3']/a");
	public final static By CDNT_LINK_CLOSE = By.xpath("//*[@id='3cf044f9-6c2e-4ab3-aeee-5af093c46053']/a");
	public final static By CDNT_SPAN_NOTE_FOR = By.id("ParentChildContainer_CustomerDetails_Notes1_lblNoteFor");
	public final static By CDNT_SELECT_NOTE_FOR = By.id("ParentChildContainer_CustomerDetails_Notes1_cboConsumerType");
	public final static By CDNT_TD_NOTE = By.xpath("//*[@id='dvNew']/table[2]/tbody/tr[2]/td[1]");
	public final static By CDNT_TEXTAREA_NOTE = By.id("ParentChildContainer_CustomerDetails_Notes1_txtNote");
	
	// the first record of notes
	public final static By CDNT_TD_RESULT = By.xpath("//*[@id='ParentChildContainer_CustomerDetails_Notes1_NotesReport_ReportOutput']/table/tbody/tr[3]/td[4]");
	
	public static void enterYourAccountCustomerDetailsNotesPage(){
		SpHomePageUtil.enter();
		SpHomePageUtil.enterYourAccountCustomerDetailsDefaultPage();
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SpYAccountCustomerDetailsDefaultPageUtil.YACD_TAB_NOTES));
		waitForNoteDefaultPageToLoad();
	}
	
	/** wait for note default page to load*/
	public static void waitForNoteDefaultPageToLoad() {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				CDNT_IMG_REPORT_PREFERENCES, CDNT_IMG_EXPORT, CDNT_INPUT_FIRST, CDNT_INPUT_PREVIOUS, CDNT_INPUT_NEXT,
				CDNT_INPUT_LAST, CDNT_IMG_FILTER_0, CDNT_IMG_FILTER_1, CDNT_IMG_FILTER_2, CDNT_IMG_FILTER_3
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_IMG_REPORT_PREFERENCES));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_IMG_EXPORT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_INPUT_FIRST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_INPUT_PREVIOUS));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_INPUT_NEXT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_INPUT_LAST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_IMG_FILTER_0));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_IMG_FILTER_1));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_IMG_FILTER_2));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(CDNT_IMG_FILTER_3));
		
	}
	/** wait for add note page to load*/
	public static void waitForAddNotePageToLoad(){
		SeleniumUtil.waitOnElements(new By[]{
				CDNT_LINK_SAVE, CDNT_LINK_CLOSE, CDNT_SPAN_NOTE_FOR, CDNT_SELECT_NOTE_FOR, CDNT_TD_NOTE, CDNT_TEXTAREA_NOTE
				});
		
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CDNT_LINK_SAVE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CDNT_LINK_CLOSE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CDNT_SPAN_NOTE_FOR));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CDNT_SELECT_NOTE_FOR));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CDNT_TD_NOTE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(CDNT_TEXTAREA_NOTE));
	}
	
	/** wait for page to refresh after add a note*/
	public static void waitForPageRefresh() {
		boolean flag = !SeleniumUtil.findElements(CDNT_IMG_REFRESH).get(0).getAttribute("style").contains("display: none");
		long before = System.currentTimeMillis();
		while(flag){
			if((System.currentTimeMillis() - before) > 30 * 1000){
				SeleniumUtil.fail("more than 30 seconds no response!");
				return;
			}else{
				flag = !SeleniumUtil.findElements(CDNT_IMG_REFRESH).get(0).getAttribute("style").contains("display: none");
			}
		}
	}
}
